package com.manage.platform.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.manage.platform.dto.RoleAllocateMenuDto;
import com.manage.platform.dto.RoleDto;
import com.manage.platform.dto.query.RolePageQuery;
import com.manage.platform.entity.RoleEntity;
import com.manage.platform.entity.RoleMenuEntity;
import com.manage.platform.exception.CommonException;
import com.manage.platform.exception.EmError;
import com.manage.platform.mapper.RoleMapper;
import com.manage.platform.utils.RPage;
import com.manage.platform.utils.TreeListMenuUtil;
import com.manage.platform.vo.MenuPartVo;
import com.manage.platform.vo.MenuTreeVo;
import com.manage.platform.vo.MenuVo;
import com.manage.platform.vo.RoleVo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * YWD
 * 2025/9/19
 */
@AllArgsConstructor
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, RoleEntity> {

    private final UserServiceImpl userServiceImpl;
    private final RoleMenuServiceImpl roleMenuServiceImpl;

    public RPage<List<RoleVo>> getRolePage(RolePageQuery query) {
        final Page<Object> page = PageHelper.startPage(query.getPage(), query.getLimit());
        List<RoleVo> list = this.baseMapper.getRolePage(query);
        //构建菜单
        list.forEach(roleVo -> {
            List<MenuPartVo> menuList = roleMenuServiceImpl.getMenuTreeByRoleIdTwo(roleVo.getId());
            if (CollectionUtil.isNotEmpty(menuList)) {
                roleVo.setMenuTreeVoList(menuList);
            }
        });
        return RPage.ok(list, query.getPage(), query.getLimit(), page.getTotal());
    }

    public RoleVo getRoleDetails(Long id) {
        RoleVo roleVo = this.baseMapper.getRoleDetails(id);
        //构建菜单
        List<MenuPartVo> menuList = roleMenuServiceImpl.getMenuTreeByRoleIdTwo(roleVo.getId());
        if (CollectionUtil.isNotEmpty(menuList)) {
            roleVo.setMenuTreeVoList(menuList);
        }
        return roleVo;
    }

    public Boolean saveRole(RoleDto dto) {
        return save(BeanUtil.copyProperties(dto, RoleEntity.class));
    }

    public Boolean updateRole(RoleDto dto) {
        return updateById(BeanUtil.copyProperties(dto, RoleEntity.class));
    }

    public Boolean deleteRole(Long id) {
        //角色是否正在使用
        Boolean b = userServiceImpl.getByRoleId(id);
        if (b) throw new CommonException(EmError.ROLE_IS_USED);
        return removeById(id);
    }

    public Boolean allocateMenu(RoleAllocateMenuDto dto) {
        //删除再新增
        roleMenuServiceImpl.removeByRoleId(dto.getRoleId());
        return roleMenuServiceImpl.allocateMenu(dto);
    }


    public List<MenuTreeVo> getRoleMenu(Long id) {
        List<MenuTreeVo> tree = roleMenuServiceImpl.getMenuTreeByRoleId(id);
        return TreeListMenuUtil.buildMenuTreeList(tree);
    }

}
